package gui;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import com.avanti.routes.Cuadrante;
import com.avanti.routes.ListaCuadrantes;

import tad.Cell;
import agents.environment.Map;

// import tad.Map;
// import tad.Singleton;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class MainWindow extends javax.swing.JFrame
{

	// Point p=new Point(50,50);

	{
		// Set Look & Feel
		try
		{
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private JPanel jPanel1;
	private Canvas canvas;
	// private Map map;
	private Image image;
	private Hashtable<String, Object> puntos;
	private Image iconoFuego;
	private Image iconoLider;
	private Image iconoDependiente;
	private Image iconoIndependiente;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	/*
	 * public static void main(String[] args) { SwingUtilities.invokeLater(new
	 * Runnable() { public void run() { MainWindow inst = new MainWindow();
	 * inst.setLocationRelativeTo(null); inst.setVisible(true); } }); }
	 */

	public MainWindow(String file)
	{
		super();
		//setDefaultCloseOperation(this.EXIT_ON_CLOSE);
		// map=Singleton.getMap();
		try
		{
			iconoFuego = ImageIO.read(new File("fire-icon.jpg"));
			iconoLider = ImageIO.read(new File("leaderIcon.jpg"));
			iconoDependiente = ImageIO.read(new File("dependentIcon.jpg"));
			iconoIndependiente = ImageIO.read(new File("independentIcon.jpg"));

		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initGUI(file);
	}

	private void initGUI(String file)
	{
		puntos = new Hashtable<String, Object>();
		try
		{
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				// jPanel1.setPreferredSize(new java.awt.Dimension(map.getH(),
				// map.getW()));}

				File inputFile = new File(file);
				try
				{
					image = ImageIO.read(inputFile);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}

				/*
				 * { canvas = new Canvas(); GridLayout canvasLayout = new
				 * GridLayout(1, 1); canvasLayout.setHgap(5);
				 * canvasLayout.setVgap(5); canvasLayout.setColumns(1);
				 * jPanel1.add(canvas); canvas.setPreferredSize(new
				 * java.awt.Dimension(map.getH(), map.getW()));
				 */
			}
			pack();
			setSize(1027, 531);

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * public void paintComponents(Graphics grafico){
	 * super.paintComponents(grafico); //ahora... a pintar!!
	 * setBackground(Color.lightGray); setForeground(Color.BLACK); //COLOR
	 * for(int i = 0; i < map.getW(); i++){ for(int j = 0; j < map.getH(); j++){
	 * if(map.getColor(i, j)=='w') grafico.setColor(Color.white);
	 * if(map.getColor(i, j)=='b') grafico.setColor(Color.black);
	 * if(map.getColor(i, j)=='g') grafico.setColor(Color.green);
	 * if(map.getColor(i, j)=='X') grafico.setColor(Color.gray);
	 * if(map.getColor(i, j)=='r') grafico.setColor(Color.red);
	 * grafico.drawRect(i, j, 1, 1); } } super.paintComponents(grafico); }
	 * 
	 * public void setPoint(Point p){ this.p=p; repaint(); }
	 */
	public void paint(Graphics grafico)
	{
		// super.paint(grafico);
		grafico.drawImage(image, 0, 0, this);

		grafico.setColor(Color.red);
		grafico.drawRect(527 - 25, 286 - 25, 50, 50);
		grafico.drawRect(138 - 25, 286 - 25, 50, 50);
		grafico.setColor(Color.blue);
		Object p;
		Enumeration<String> it = puntos.keys();
		Image icono;
		while (it.hasMoreElements())
		{
			String id = it.nextElement();

			if (id.startsWith("Leader"))
			{
				icono = iconoLider;
			}
			else if (id.startsWith("Dependent"))
			{
				icono = iconoDependiente;
			}
			else if (id.startsWith("Independent"))
			{
				icono = iconoIndependiente;
			}
			else
			{
				icono = iconoFuego;
			}
			
//			// Read properties file.
//			Properties properties = new Properties();
//			try {
//			    properties.load(new FileInputStream("agentes.properties"));
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent1.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent1.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent2.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent2.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent3.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent3.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent4.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent4.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent5.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent5.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent6.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent6.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent7.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent7.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent8.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent8.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent9.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent9.y")) * Cell.tamCelda, null);
//			grafico.drawImage(icono, Integer.parseInt(properties.getProperty("Dependent10.x")) * Cell.tamCelda, Integer.parseInt(properties.getProperty("Dependent10.y")) * Cell.tamCelda, null);

			
			p = puntos.get(id);
			
			if (p instanceof Point)
			{
				grafico.drawImage(icono, ((Point) p).x * Cell.tamCelda, ((Point) p).y * Cell.tamCelda, null);
				// grafico.drawImage(icono,((Point) p).x * Cell.tamCelda,
				// (((Point) p).y * Cell.tamCelda) + Cell.tamCelda);
			}
			else if (p instanceof List)
			{
				// Puntos del fuego
				List<Point> listaPuntos = (List<Point>) p;
				// grafico.setColor(Color.red);
				for (Iterator<Point> iterator = listaPuntos.iterator(); iterator.hasNext();)
				{
					Point point = iterator.next();
					grafico.drawImage(icono, point.x * Cell.tamCelda, point.y * Cell.tamCelda, null);
				}
			}
			// grafico.drawImage(img, x, y, observer)
		}

		// ahora... a pintar!!
		/*
		 * setBackground(Color.lightGray); setForeground(Color.BLACK); //COLOR
		 * for(int i = 0; i < map.getW(); i++){ for(int j = 0; j < map.getH();
		 * j++){ if(map.getColor(i, j)=='w') grafico.setColor(Color.white);
		 * if(map.getColor(i, j)=='b') grafico.setColor(Color.black);
		 * if(map.getColor(i, j)=='g') grafico.setColor(Color.green);
		 * if(map.getColor(i, j)=='X') grafico.setColor(Color.gray);
		 * if(map.getColor(i, j)=='r') grafico.setColor(Color.red);
		 * grafico.drawRect(i, j, 1, 1); } } grafico.setColor(Color.blue);
		 * grafico.drawOval(p.x, p.y, 25, 25);
		 */

		grafico.setColor(Color.red);

		// TODO Prueba pintar cuadrantes
//		 List cuadrantes = ListaCuadrantes.getInstance().getLista();
//		 for (Iterator iterator = cuadrantes.iterator(); iterator.hasNext();)
//		 {
//		 Cuadrante c = (Cuadrante) iterator.next();
//		 //Izq
//		 grafico.drawLine(c.getPosNW().x * Cell.tamCelda, c.getPosNW().y *
//		 Cell.tamCelda,c.getPosNW().x * Cell.tamCelda, (c.getPosSE().y *
//		 Cell.tamCelda) + Cell.tamCelda);
//		 //Arriba
//		 grafico.drawLine(c.getPosNW().x * Cell.tamCelda, c.getPosNW().y *
//		 Cell.tamCelda,(c.getPosSE().x * Cell.tamCelda) + Cell.tamCelda,
//		 c.getPosNW().y * Cell.tamCelda);
//		 grafico.drawLine(c.getPosSE().x * Cell.tamCelda, c.getPosNW().y *
//		 Cell.tamCelda,c.getPosSE().x * Cell.tamCelda, (c.getPosSE().y *
//		 Cell.tamCelda) + Cell.tamCelda);
//		 grafico.drawLine(c.getPosNW().x * Cell.tamCelda, c.getPosSE().y *
//		 Cell.tamCelda,(c.getPosSE().x * Cell.tamCelda) + Cell.tamCelda,
//		 c.getPosSE().y * Cell.tamCelda);
//		 //
//					
//		 grafico.drawString(""+ c.getID(),(c.getPosNW().x * Cell.tamCelda) +
//		 30 , (c.getPosNW().y * Cell.tamCelda) + 30);
//		 }

		// ////System.out.println("begin paint");
		//
		//		
		// grafico.setColor(color.orange);
		//
//
//		int t = Cell.tamCelda;
//		Cell[][] celdas = Map.getInstance().celdas;
//
//		for (int i = 0; i < celdas.length; i++)
//		{
//			for (int j = 0; j < celdas[j].length; j++)
//			{
//				grafico.drawString("" + celdas[i][j].getBusy(), i * t + (t / 2), (j * t) + t);
//				grafico.drawLine(i * t, j * t, i * t + t, j * t);
//				grafico.drawLine(i * t, j * t, i * t, j * t + t);
//			}
//		}
	}

	public void addPoint(String id, Point p)
	{
		puntos.put(id, p);
		repaint();
	}

	public void addPoints(String id, List<Point> points)
	{
		puntos.put(id, points);
		repaint();
	}
	


}
